/**
 * ProvinceService.java 
 * Copyright © 2015-3015, 杭州泊享网络科技有限公司
 * 
 * @author Liu Tao
 * @create 2013年8月14日
 */
package com.pshare.artemis.basic.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pshare.artemis.basic.bean.Province;
import com.pshare.artemis.basic.mapper.ProvinceMapper;
import com.pshare.framework.mapper.BaseMapper;
import com.pshare.framework.service.BaseService;
import com.pshare.tool.StaticMapStore;

@Service
@Transactional
public class ProvinceService extends BaseService<Integer, Province> {

	private static final Logger logger = Logger.getLogger(ProvinceService.class);
	
	@Autowired
	private ProvinceMapper provinceMapper;

	@Override
	protected BaseMapper<Integer, Province> getBaseMapper() {
		return provinceMapper;
	}

    public List<Province> queryAll() {
        boolean bExist = StaticMapStore.getInstance().containsKey(StaticMapStore.Key.PROVINCE);
        if (bExist) {
            Object rs = StaticMapStore.getInstance().get(StaticMapStore.Key.PROVINCE);
            return (List<Province>) rs;
        }
        return provinceMapper.queryAll();
    }
    
    /**
     * 根据省ID从环城中读取省名称
     * @param provinceId
     * @return String
     */
    public String getProinceName(Integer provinceId) {
    	if (provinceId == null) {
    		return null;
    	}
    	boolean bExist = StaticMapStore.getInstance().containsKey(StaticMapStore.Key.PROVINCEMAP);
        if (bExist) {
            Map<Integer, String> rsMap = (Map<Integer, String>) StaticMapStore.getInstance().get(StaticMapStore.Key.PROVINCEMAP);
            return rsMap.get(provinceId) == null ? "" : rsMap.get(provinceId);
        }
        return "";
    }
    
    public Province get(Integer id) {
    	return provinceMapper.get(id);
    }
    
	/**
	 * 根据名称查找省ID
	 * @param name
	 * @return Integer
	 */
    public Integer findIdByName(String name) {
    	if (StringUtils.isBlank(name)) {
    		logger.warn("name is blank");
    		return null;
    	}
    	return provinceMapper.findIdByName(name);
    }
    
    public Province findByName(String name) {
    	if (StringUtils.isBlank(name)) {
    		logger.warn("name is blank");
    		return null;
    	}
    	return provinceMapper.findByName(name);
    }

}
